package mylistdemo001;

import java.util.ArrayList;
import java.util.Scanner;

/**添加用户对象并判断是否存在
 * 需求：
 * 1.main方法中定义一个集合,存入三个集合对象
 * 用户属性为：id,username,password
 * 2.要求：定义一个方法，根据id查找对应的用户信息。
 * 如果存在返回索引
 * 如果不存在，返回索引-1
 */
public class ArrayListPractice06 {
    public static void main(String[] args) {
        ArrayList<User> list = new ArrayList<>();

        User u = new User(001,"张三","2016lcm");
        User u1 = new User(002,"李四","20216lcm");
        User u2 = new User(003,"王二","20156lcm");
        list.add(u);
        list.add(u1);
        list.add(u2);
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要查找的id");
        int id = sc.nextInt();
        int index = looUp(list,id);
        System.out.println("索引为："+index);

     }

     public static int looUp(ArrayList<User> list,int id){
         for (int i = 0; i < list.size(); i++) {
             User u = list.get(i);
             int a = u.getId();
             if (a==id){
                 return i;
             }
         }
        return -1;
     }
}
